package cn.itcast.user.service.impl;

import cn.itcast.hmall.pojo.user.Address;
import cn.itcast.user.mapper.AddressMapper;
import cn.itcast.user.service.AddressService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired
    private AddressMapper addressMapper;
    /**
     * 根据用户id获得用户所有地址
     * @param userId
     * @return
     */
    @Override
    public List<Address> addressById(Long userId) {
        if(userId==null||"".equals(userId)){
            throw new RuntimeException("从线程中获取用户id失败");
        }
        LambdaQueryWrapper<Address> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Address::getUserId,userId);
        List<Address> list = addressMapper.selectList(wrapper);
        return list;
    }

    /**
     * 根据地址id获得详细地址
     * @param addressId
     * @return
     */
    @Override
    public Address selectAddressById(Long addressId) {
        if(addressId==null||"".equals(addressId)){
            throw new RuntimeException("地址id不能为空");
        }
        return addressMapper.selectById(addressId);
    }
}
